const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const utils = require('../../utils.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberDTOs:[],
    selectMemberList: [],
    selectValue: [],
    content: '',
    ciId: '',
    cgid: '',
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      ciId: options.ciId
    })

    this.setData({
      cgid: options.cgid
    })

    this.setData({
      content:options.name
    })

    this.obtainClassMembers()
  },

  // 编辑群组
  editGroup(){
    utils.ajaxFn({
      url: config.service.modifyGroup,
      body: {
        cgId: this.data.cgid,
        name: this.data.content
      },
      callback: (data) => {
        this.assignGroupMembers()
      }
    })
  },

  // 获取成员列表
  obtainClassMembers() {
    utils.ajaxFn({
      url: config.service.obtainClassMembers,
      body: { 'ciId': this.data.ciId },
      callback: (data) => {
        if (data.memberDTOs.length < 1) {
          wx.showModal({
            title: '提示',
            content: "当前班级没有成员，请添加！"
          })

          return false;
        }

        this.setData({
          memberDTOs: data.memberDTOs
        })

        this.obtainGroupMembers()
      }
    })
  },

  // 删除成员
  deleteManage(e) {
    let dataset = e.currentTarget.dataset
    let selectValue = this.data.selectValue
    let memberDTOs = this.data.memberDTOs
    selectValue.splice(dataset.index, 1)

    this.setData({
      selectValue: selectValue
    })


    memberDTOs.forEach(value => {
      if (value.cmId === dataset.cmid) {
        value.state = false
      }
    })

    this.setData({
      memberDTOs: memberDTOs
    })

    console.log(this.data.memberDTOs)
  },


  // 选择成员
  checkboxChange(e) {
    let iarr = []
    this.data.memberDTOs.forEach(value => {
      if (e.detail.value.indexOf('' + value.uId) > -1) {
        value.state = true
        iarr.push(value)
      } else {
        value.state = false
      }
    })

    this.setData({
      selectValue: iarr
    })

    this.setData({
      selectMemberList: e.detail.value
    })
  },

  // 指定群组成员
  assignGroupMembers() {
    utils.ajaxFn({
      url: config.service.assignGroupMembers,
      body: {
        cgId: this.data.cgid,
        uIdsJn: `[${this.data.selectMemberList}]`
      },
      callback: (data) => {
        wx.navigateTo({
          url: `/pages/groupManager/groupManager`
        })
      }
    })
  },

  // 获取群组成员
  obtainGroupMembers(){
    utils.ajaxFn({
      url: config.service.obtainGroupMembers,
      body: {
        cgId: this.data.cgid
      },
      callback: (data) => {
        let memberList = this.data.memberDTOs
        let iarr = data.memberDTOs.map(value => value.uId )

        this.setData({
          selectValue: data.memberDTOs
        })

        memberList.map(value =>{
          value.state = iarr.indexOf(value.uId) > -1
          return value
        })

        this.setData({
          memberDTOs: memberList
        })
      }
    })
  },
  
  // 删除群组
  removeGroups(){
    utils.ajaxFn({
      url: config.service.removeGroups,
      body: {
        cgIdsJn: `[${this.data.cgid}]`
      },
      callback: (data) => {        wx.navigateTo({
          url: `/pages/groupManager/groupManager`
        })
      }
    })
  },

  //群组标题
  bindKeyInput(e) {
    this.setData({
      content: e.detail.value
    })
  }
})